package com.justgame.project.cloud.common.util;

import com.aliyun.oss.OSS;
import com.justgame.project.cloud.common.constant.Const;
import com.justgame.project.cloud.common.enums.FileTypeEnum;

/**
 * OSS字符串处理工具
 */
public class AliyunOssUtil {
    public static OSS getOss() {
        return AppContextUtils.getBean(OSS.class);
    }

    public static String getEndpoint() {
        return Const.OSS_ENDPOINT;
    }

    public static String getKeyId() {
        return Const.OSS_ACCESS_KEY_ID;
    }

    public static String getSecret() {
        return Const.OSS_ACCESS_KEY_SECRET;
    }

    public static String getBucket() {
        return Const.OSS_BUCKET_NAME;
    }


    public static String getHeaderPath() {
        return String.format("https://%s.%s/", Const.OSS_BUCKET_NAME, Const.OSS_ENDPOINT);
    }

    public static String getOssFileName(String url) {
        return url.split(getHeaderPath())[1];
    }

    /**
     * such as :
     * You give me : type ( Image ) and file Name ( apple.png )
     * and then this method give you a String data like : Image/apple.png
     * What this means is : Image/apple.png  -> Image is dir , and the apple.png is a file name that is what you need
     *
     * @param typeEnum Annotation
     * @param fileName the file name of string
     * @author ZhouWeiBo ( in chinese: 周炜博 )
     */
    public static String buildOssFileName(FileTypeEnum typeEnum, String fileName) {
        return String.format("%s/%s", typeEnum.getEnumKey(), ObjUtil.removeAllBlank(fileName));
    }

    public static String buildOssFileName(String categoryName, String fileName) {
        return String.format("%s/%s", categoryName, ObjUtil.removeAllBlank(fileName));
    }

    public static String buildFinalPath(String ossFileName) {
        return String.format("%s%s", getHeaderPath(), ossFileName);
    }

    /**
     * such as :
     * You give me : URL ( just like :  https://web.app.com/Image/apple.png )
     * and then this method give you a String file name like : apple.png
     * !!! but we dont know this file name is really have file type !!!
     *
     * @param url String
     * @author ZhouWeiBo ( in chinese: 周炜博 )
     */
    public static String getOriginFileName(String url) {
        String objName = getOssFileName(url);
        if (!objName.contains("/")) {
            return objName;
        } else {
            return FileUtils.getFileUrlSuffix(objName);
        }
    }

}
